package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.SysModuleBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.SysModuleMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.serviceManage.utils.RedisUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

//import redis.clients.jedis.Jedis;

public class SysModuleUpdate implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        @SuppressWarnings("rawtypes")
        MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);

//		Jedis redis = null;
        try {
            SysModuleBean bean = JsonUtils.objectToPojo(req.getData(), SysModuleBean.class);
            SysModuleMapper mapper = factory.getMapper(SysModuleMapper.class);
            // 查询原数据，比较是否修改父级目录
            SysModuleBean oldBean = mapper.getSysMonduleById(bean.getModuleId() + "");

            if (bean.getModuleId() < 0 || oldBean == null) {
                info.setCode(Constant.ILLEGAL_PARAMETER);
                info.setDescription("模块id信息为空，或者查无当前模块信息");
                return info;
            }
            if (!(oldBean.getParent() + "").equals((bean.getParent() + ""))) {
                // 重新生成code
                SysModuleAdd.handCode(mapper, bean);
            } else {
                bean.setModuleCode(oldBean.getModuleCode());
            }
            mapper.updateSysMondule(bean);
//			redis = RedisUtils.getJedis();
            RedisUtils.del(Constant.SUB_MODULE);
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_INT_FAIL);
            info.setDescription(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        return info;
    }

}
